from flask import Flask, request, jsonify
import os,requests,jsonpath

app = Flask(__name__)


# 保存上传的图片到指定目录
def save_uploaded_photo(photo):
    # 指定保存图片的目录
    upload_dir = 'uploads'
    os.makedirs(upload_dir, exist_ok=True)  # 如果目录不存在，则创建目录

    # 保存图片到指定目录
    photo_path = os.path.join(upload_dir, photo.filename)
    photo.save(photo_path)

    return photo_path


# 处理上传图片的接口
@app.route('/upload_photo', methods=['POST'])
def upload_photo():
    if 'photo' not in request.files:
        return jsonify({'error': 'No photo provided'}), 400

    photo = request.files['photo']

    if photo.filename == '':
        return jsonify({'error': 'Empty filename provided'}), 400

    # 保存上传的图片
    saved_photo_path = save_uploaded_photo(photo)

    return jsonify({'message': 'Photo uploaded successfully', 'photo_path': saved_photo_path}), 200


if __name__ == '__main__':
    app.run(debug=True)
